package com.yeyks.commonReference.service.vip.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.yeyks.common.em.pay.PayTypeEnum;
import com.yeyks.common.em.vip.MemberCardEnableOrderStatusEnum;
import com.yeyks.common.em.vip.MemberCardEnableRecordStatusEnum;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.IdWorkerUtil;
import com.yeyks.common.utils.RedisUtil;
import com.yeyks.commonReference.append.alipay.config.AlipayNotifyParam;
import com.yeyks.commonReference.append.pay.PayService;
import com.yeyks.commonReference.append.sms.SmsService;
import com.yeyks.commonReference.pojo.param.vip.MemberCardOnline.MemberCardOnlineSetPasswordParam;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.consumer.MemberCardService;
import com.yeyks.commonReference.service.consumer.UserMemberCardService;
import com.yeyks.commonReference.service.order.OrderHeadService;
import com.yeyks.commonReference.service.vip.MemberCardEnableOrderService;
import com.yeyks.commonReference.service.vip.MemberCardEnableRecordService;
import com.yeyks.commonReference.service.vip.MemberCardOnlineService;
import com.yeyks.consumer.dal.domain.MemberCard;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.dal.domain.UserMemberCard;
import com.yeyks.consumer.dto.UserMemberCardDTO;
import com.yeyks.consumer.param.UserMemberCard.UserMemberCardBindCardParam;
import com.yeyks.consumer.param.UserMemberCard.UserMemberCardInfoParam;
import com.yeyks.consumer.service.RechargeRecordService;
import com.yeyks.consumer.service.UserInfoService;
import com.yeyks.vip.dal.domain.MemberCardEnableOrder;
import com.yeyks.vip.dal.domain.MemberCardEnableRecord;
import com.yeyks.vip.dal.domain.MemberCardSellRelation;
import com.yeyks.vip.dal.domain.MemberCardSellRelationLevel;
import com.yeyks.vip.param.MemberCardEnableRecordCreateOrderPayRecordParam;
import com.yeyks.vip.param.MemberCardOnlinePlaceAnOrderServiceParam;
import com.yeyks.vip.service.MemberCardSellRelationLevelService;
import com.yeyks.vip.service.MemberCardSellRelationService;
import com.yeyks.vip.vo.MemberCardEnableOrderPlaceAnOrderVo;
import com.yeyks.vip.vo.MemberCardOnlineCheckEnableCardVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Service
public class MemberCardOnlineServiceImpl implements MemberCardOnlineService {

    @Autowired
    private AccountInfoService accountInfoService;
    @Autowired
    private MemberCardService memberCardService;
    @Autowired
    private MemberCardEnableOrderService memberCardEnableOrderService;
    @Autowired
    private MemberCardEnableRecordService memberCardEnableRecordService;
    @Autowired
    private MemberCardSellRelationService memberCardSellRelationService;
    @Autowired
    private PayService payService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OrderHeadService orderHeadService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RechargeRecordService rechargeRecordService;
    @Autowired
    private UserMemberCardService userMemberCardService;
    @Autowired
    private MemberCardSellRelationLevelService memberCardSellRelationLevelService;

    //检查是否有卡可以出售v3.8.3
    @Override
    public MemberCardOnlineCheckEnableCardVo checkEnableCard(Integer cardType) {
        MemberCardOnlineCheckEnableCardVo vo = new MemberCardOnlineCheckEnableCardVo();
        vo.setFlag(false);
        //查询可用的卡
        MemberCard memberCard = getEnableCard(cardType);
        if (Objects.nonNull(memberCard)) {
            vo.setFlag(true);
            vo.setSalePrice(memberCard.getSalePrice());
            vo.setRemainAmount(memberCard.getRemainAmount());
        }
        return vo;
    }

    //获取一张可以出售的卡v3.8.3
    @Override
    public MemberCard getEnableCard(Integer cardType) {
        //查询可用的卡
        MemberCard memberCard = memberCardService.getOne(
                new QueryWrapper<MemberCard>()
                        .eq(MemberCard.ENABLE, 0)
                        .eq(MemberCard.STATUS, 1)
                        .in(MemberCard.CARD_TYPE, cardType)
                        .in(MemberCard.SALE_TYPE, 2)
                        .last("limit 1")
        );
        return memberCard;
    }

    //下单v3.8.3
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemberCardEnableOrderPlaceAnOrderVo placeAnOrder(MemberCardOnlinePlaceAnOrderServiceParam param) {
        //参数校验
        switch (param.getCardType()) {
            case 1:
                throw new ServiceException("类型错误");
//                if (StrUtil.isBlank(param.getAddress())) {
//                    throw new ServiceException("地址不能为空");
//                }
//                break;
        }
        //
        if (Objects.equals(param.getSign(), 1)) {
            UserInfo userInfo = userInfoService.selectUseInfoPhone(param.getPhone());
            if (Objects.nonNull(userInfo)) {
                if (rechargeRecordService.isRecharge(userInfo)) {
                    throw new ServiceException("您已经是推手，无序重复加入");
                }
            }
        }
        //判断登录用户是否绑卡
        if (Objects.nonNull(param.getUserId())) {
            UserMemberCardDTO userMemberCardDTO = userMemberCardService.getDTOByUserId(
                    new UserMemberCardInfoParam()
                            .setUserId(param.getUserId())
                            .setCardType(param.getCardType())
            );
            if (Objects.nonNull(userMemberCardDTO)) {
                throw new ServiceException("您已拥有该类型会员卡");
            }
        }
        //判断卡是否可用
        MemberCard memberCard = getEnableCard(param.getCardType());
        if (Objects.isNull(memberCard)) {
            throw new ServiceException("会员卡已售罄");
        }
        //生成订单号
        String orderNo = "vipEnable" + IdWorkerUtil.nextStrId();
        //添加订单
        MemberCardEnableOrder memberCardEnableOrder = new MemberCardEnableOrder();
        memberCardEnableOrder.setOrderNo(orderNo);
        memberCardEnableOrder.setStatus(MemberCardEnableOrderStatusEnum.pay_pre.value());
        memberCardEnableOrder.setCreateTime(new Date());
        memberCardEnableOrder.setCardType(param.getCardType());
        memberCardEnableOrder.setSign(param.getSign());
        if (CompareUtil.compare(memberCard.getSalePrice(), 0) <= 0) {
            throw new ServiceException("会员卡异常");
        }
        if (Objects.equals(param.getSign(), 3)) {
            memberCardEnableOrder.setPayValue(1990);
        } else if (Objects.equals(param.getMakerLevel(), 1)) {
            memberCardEnableOrder.setPayValue(9900);
        } else if (Objects.equals(param.getMakerLevel(), 2)) {
            memberCardEnableOrder.setPayValue(99000);
        } else if (Objects.equals(param.getMakerLevel(), 3)) {
            memberCardEnableOrder.setPayValue(198000);
        } else {
            memberCardEnableOrder.setPayValue(memberCard.getSalePrice());
        }
        boolean save = memberCardEnableOrderService.save(memberCardEnableOrder);
        if (!save) {
            throw new ServiceException("订单生成失败");
        }
        switch (param.getCardType()) {
//            case 1:
//                //存储用户信息
//                MemberCardGiftInfo memberCardGiftInfo = new MemberCardGiftInfo();
//                memberCardGiftInfo.setOrderNo(orderNo);
//                memberCardGiftInfo.setName(param.getName());
//                memberCardGiftInfo.setPhone(param.getPhone());
//                memberCardGiftInfo.setAddress(param.getAddress());
//                memberCardGiftInfo.setStatus(0);
//                memberCardGiftInfo.setCreatedTime(new Date());
//                save = memberCardGiftInfoService.save(memberCardGiftInfo);
//                if (!save) {
//                    throw new ServiceException("用户信息存储失败");
//                }
//                break;
            case 2:
                //销售关系
                MemberCardSellRelation memberCardSellRelation = new MemberCardSellRelation();
                memberCardSellRelation.setOrderNo(orderNo);
                if (Objects.nonNull(param.getUserId())) {
                    UserInfo userInfo = userInfoService.getById(param.getUserId());
                    memberCardSellRelation.setName(userInfo.getNickname());
                    memberCardSellRelation.setPhone(userInfo.getPhone());
                    memberCardSellRelation.setPassword(param.getPassword());
                    memberCardSellRelation.setInviteId(param.getInviteId());
                } else {
                    memberCardSellRelation.setName(param.getName());
                    memberCardSellRelation.setPhone(param.getPhone());
                }
                memberCardSellRelation.setType(2);
                memberCardSellRelation.setStatus(1);
                save = memberCardSellRelationService.save(memberCardSellRelation);
                if (!save) {
                    throw new ServiceException("用户信息存储失败");
                }
                //创客关系v4.3.0
                if (Objects.nonNull(param.getMakerLevel())) {
                    MemberCardSellRelationLevel memberCardSellRelationLevel = BeanUtil.toBean(param, MemberCardSellRelationLevel.class);
                    memberCardSellRelationLevel.setOrderNo(orderNo);
                    save = memberCardSellRelationLevelService.save(memberCardSellRelationLevel);
                    if (!save) {
                        throw new ServiceException("创客信息存储失败");
                    }
                }
                break;
        }
        return new MemberCardEnableOrderPlaceAnOrderVo().setOrderNo(orderNo);
    }

    //根据订单生成支付信息和支付记录v3.8.3
    @Override
    public Map<String, Object> createOrderPayRecord(MemberCardEnableRecordCreateOrderPayRecordParam param) {
        String payTypeDesc = PayTypeEnum.getDesc(param.getPayTypeInt());
        MemberCardEnableOrder memberCardEnableOrder = memberCardEnableOrderService.getOne(
                new QueryWrapper<MemberCardEnableOrder>()
                        .eq(MemberCardEnableOrder.ORDER_NO, param.getOrderNo())
                        .last("limit 1")
        );
        if (null == memberCardEnableOrder || !memberCardEnableOrder.getStatus().equals(MemberCardEnableOrderStatusEnum.pay_pre.value())) {
            throw new ServiceException("订单不存在或者订单已付款");
        }
        //生成支付信息
        Map<String, Object> map = payService.memberCardEnableorderPayOnline(payTypeDesc, param.getOrderNo(), param.getWxOpenId(), param.getIp());
        //存储支付记录
        try {
            MemberCardEnableRecord memberCardEnableRecord = memberCardEnableRecordService.getOne(
                    new QueryWrapper<MemberCardEnableRecord>()
                            .eq(MemberCardEnableRecord.ORDER_NO, param.getOrderNo())
                            .eq(MemberCardEnableRecord.PAY_TYPE, param.getPayTypeInt())
                            .last("limit 1")
            );
            if (Objects.isNull(memberCardEnableRecord)) {
                Date date = new Date();
                memberCardEnableRecord = new MemberCardEnableRecord();
                memberCardEnableRecord.setOrderNo(param.getOrderNo());
                memberCardEnableRecord.setStatus(MemberCardEnableRecordStatusEnum.pay_pre.value());
                memberCardEnableRecord.setPayType(param.getPayTypeInt());
                memberCardEnableRecord.setStartTime(date);
                memberCardEnableRecord.setPayAmount(memberCardEnableOrder.getPayValue());
                memberCardEnableRecord.setCreateTime(date);
                memberCardEnableRecord.setUpdateTime(date);
                boolean save = memberCardEnableRecordService.save(memberCardEnableRecord);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //卡初始密码设置
    @Override
    public void setPassword(MemberCardOnlineSetPasswordParam param) {
        boolean update = userMemberCardService.update(
                new UpdateWrapper<UserMemberCard>()
                        .set(UserMemberCard.PAY_PASSWORD, param.getPassword())
                        .eq(UserMemberCard.USER_ID, param.getUserId())
                        .eq(UserMemberCard.CARD_TYPE, param.getCardType())
                        .isNull(UserMemberCard.PAY_PASSWORD)
        );
        if (!update) {
            throw new ServiceException("卡初始密码设置失败");
        }
    }

    //会员卡enable订单支付回调v3.8.3
    @Override
    public void payCallback(String orderNo, PayTypeEnum payTypeEnum, Object resultCode, Boolean succeeded) {
        try {
            payCallbackT(orderNo, payTypeEnum, resultCode, succeeded);
        } catch (Exception e) {
            log.error("会员卡enable订单支付回调:抛错:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{},e:{}"
                    , orderNo, payTypeEnum, resultCode, succeeded, e.getMessage());
        }
    }

    //会员卡enable订单支付回调v3.8.3

    /**
     * {@link MemberCardEnableRecordServiceImpl#payCallbackT(java.lang.String, com.yeyks.common.em.pay.PayTypeEnum, com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult, java.lang.Boolean)}
     */
    @Transactional(rollbackFor = Exception.class)
    public void payCallbackT(String orderNo, PayTypeEnum payTypeEnum, Object resultCode, Boolean succeeded) {
        //修改订单
        MemberCardEnableOrder memberCardEnableOrder = memberCardEnableOrderService.getOne(
                new QueryWrapper<MemberCardEnableOrder>()
                        .eq(MemberCardEnableOrder.ORDER_NO, orderNo)
                        .last("limit 1")
        );
        if (null != memberCardEnableOrder && MemberCardEnableOrderStatusEnum.pay_pre.value().equals(memberCardEnableOrder.getStatus())) {
            redisUtil.runWithLock("vip:memberCard:enableOrderPay:online", () -> {
                //抽取卡
                MemberCard memberCard = getEnableCard(memberCardEnableOrder.getCardType());
                if (Objects.nonNull(memberCard)) {
                    //更新订单
                    boolean update = memberCardEnableOrderService.update(
                            new UpdateWrapper<MemberCardEnableOrder>()
                                    .eq(MemberCardEnableOrder.ID, memberCardEnableOrder.getId())
                                    .eq(MemberCardEnableOrder.CARD_TYPE, memberCardEnableOrder.getCardType())
                                    .set(MemberCardEnableOrder.OVER_TIME, new Date())
                                    .set(MemberCardEnableOrder.CARD_NO, memberCard.getCardNo())
                                    .set(MemberCardEnableOrder.STATUS, (
                                                    succeeded ?
                                                            MemberCardEnableOrderStatusEnum.pay_success.value()
                                                            : MemberCardEnableOrderStatusEnum.pay_fail.value()
                                            )
                                    ).set(MemberCardEnableOrder.PAYMENT_METHOD, payTypeEnum.value())
                    );
                    if (!update) {
                        log.error("会员卡enable订单支付回调:订单更新失败:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
                                , orderNo, payTypeEnum.desc(), resultCode, succeeded);
                    }
                    //修改订单支付记录
                    MemberCardEnableRecord memberCardEnableRecord = memberCardEnableRecordService.getOne(
                            new QueryWrapper<MemberCardEnableRecord>()
                                    .eq(MemberCardEnableRecord.ORDER_NO, orderNo)
                                    .eq(MemberCardEnableRecord.PAY_TYPE, payTypeEnum.value())
                                    .eq(MemberCardEnableRecord.STATUS, MemberCardEnableRecordStatusEnum.pay_pre.value())
                                    .last("limit 1")
                    );
                    if (null != memberCardEnableRecord) {
                        //更新订单记录
                        String shangHuHao = null;
                        String guoQiShiJian = null;
                        try {
                            if (resultCode instanceof WxPayOrderNotifyResult) {
                                WxPayOrderNotifyResult wxPayOrderNotifyResult = (WxPayOrderNotifyResult) resultCode;
                                shangHuHao = wxPayOrderNotifyResult.getTransactionId();
                                guoQiShiJian = wxPayOrderNotifyResult.getTimeEnd();
                            } else if (resultCode instanceof AlipayNotifyParam) {
                                AlipayNotifyParam alipayNotifyParam = (AlipayNotifyParam) resultCode;
                                shangHuHao = alipayNotifyParam.getTradeNo();
                                guoQiShiJian = DatePattern.PURE_DATETIME_FORMAT.format(alipayNotifyParam.getGmtRefund());
                            }
                        } catch (Exception e) {
                        }
                        update = memberCardEnableRecordService.update(
                                new UpdateWrapper<MemberCardEnableRecord>()
                                        .eq(MemberCardEnableRecord.ID, memberCardEnableRecord.getId())
                                        .set(MemberCardEnableRecord.STATUS,
                                                (
                                                        succeeded ?
                                                                MemberCardEnableRecordStatusEnum.pay_success.value()
                                                                : MemberCardEnableRecordStatusEnum.pay_fail.value()
                                                )
                                        )
                                        .set(MemberCardEnableRecord.PAY_RETURN_NO, shangHuHao)
                                        .set(MemberCardEnableRecord.FINISH_TIME, guoQiShiJian)
                                        .set(MemberCardEnableRecord.PAY_RESULT, JSONUtil.toJsonStr(resultCode))
                        );
                        if (!update) {
                            log.error("会员卡enable订单支付回调:订单支付记录更新失败:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
                                    , orderNo, payTypeEnum.desc(), resultCode, succeeded);
                        }
                    } else {
                        log.error("会员卡enable订单支付回调:订单支付记录不存在:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
                                , orderNo, payTypeEnum.desc(), resultCode, succeeded);
                    }
                    //更新卡enable
                    if (succeeded) {
                        if (null != memberCard) {
                            update = memberCardService.update(
                                    new UpdateWrapper<MemberCard>()
                                            .eq(MemberCard.ID, memberCard.getId())
                                            .set(MemberCard.ENABLE, 1)
                            );
                            if (!update) {
                                log.error("会员卡enable订单支付回调:卡enable更新失败:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
                                        , orderNo, payTypeEnum.desc(), resultCode, succeeded);
                            }
                        } else {
                            log.error("会员卡enable订单支付回调:卡不存在:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
                                    , orderNo, payTypeEnum.desc(), resultCode, succeeded);
                        }
                    }
                    //更新附加关系
                    if (succeeded) {
                        switch (memberCardEnableOrder.getCardType()) {
//                        case 1:
//                            //更新礼物状态
//                            MemberCardGiftInfo memberCardGiftInfo = memberCardGiftInfoService.getOne(
//                                    new QueryWrapper<MemberCardGiftInfo>()
//                                            .eq(MemberCardGiftInfo.ORDER_NO, orderNo)
//                                            .last("limit 1")
//                            );
//                            if (Objects.nonNull(memberCardGiftInfo)) {
//                                update = memberCardGiftInfoService.update(
//                                        new UpdateWrapper<MemberCardGiftInfo>()
//                                                .eq(MemberCardGiftInfo.ID, memberCardGiftInfo.getId())
//                                                .set(MemberCardGiftInfo.STATUS, 1)
//                                );
//                                if (!update) {
//                                    log.error("会员卡enable订单支付回调:礼物记录更新失败:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
//                                            , orderNo, payTypeEnum.desc(), resultCode, succeeded);
//                                }
//                            } else {
//                                log.error("会员卡enable订单支付回调:礼物记录不存在:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
//                                        , orderNo, payTypeEnum.desc(), resultCode, succeeded);
//                            }
//                            break;
                            case 2:
                                //更新用户记录关系
                                MemberCardSellRelation memberCardSellRelation = memberCardSellRelationService.getOne(
                                        new QueryWrapper<MemberCardSellRelation>()
                                                .eq(MemberCardSellRelation.ORDER_NO, orderNo)
                                                .orderByDesc(MemberCardSellRelation.ID)
                                                .last("limit 1")
                                );
                                if (Objects.nonNull(memberCardSellRelation)) {
                                    UserInfo userInfo = userInfoService.selectUseInfoPhone(memberCardSellRelation.getPhone());
                                    update = memberCardSellRelationService.update(
                                            new UpdateWrapper<MemberCardSellRelation>()
                                                    .eq(MemberCardSellRelation.ID, memberCardSellRelation.getId())
                                                    .set(MemberCardSellRelation.STATUS, 2)
                                                    .set(MemberCardSellRelation.CARD_NO, memberCard.getCardNo())
                                    );
                                    if (!update) {
                                        log.error("会员卡enable订单支付回调:jip关系更新失败:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
                                                , orderNo, payTypeEnum.desc(), resultCode, succeeded);
                                    }
                                    if (CompareUtil.compare(memberCardEnableOrder.getSign(), 0) > 0) {
                                        //绑卡
                                        UserMemberCardBindCardParam userMemberCardBindCardParam = new UserMemberCardBindCardParam();
                                        userMemberCardBindCardParam.setUserId(userInfo.getId());
                                        userMemberCardBindCardParam.setCardType(memberCardEnableOrder.getCardType());
                                        userMemberCardBindCardParam.setCardNo(memberCard.getCardNo());
                                        userMemberCardBindCardParam.setPassword(memberCard.getPassword());
//                                        userMemberCardBindCardParam.setPayPassword(memberCardSellRelation.getPassword());
                                        userMemberCardBindCardParam.setPayPassword(null);
                                        userMemberCardBindCardParam.setInviteId(memberCardSellRelation.getInviteId());
                                        userMemberCardService.userBindCard(userMemberCardBindCardParam);
                                    } else {
                                        HashMap<String, String> map = new HashMap<>();
                                        map.put("phone", memberCardSellRelation.getPhone());
                                        map.put("card_no", memberCard.getCardNo());
                                        map.put("password", memberCard.getPassword());
                                        smsService.sendSmsToBoolean(map, "SMS_181211129");
                                    }
                                    if (Objects.equals(memberCardEnableOrder.getSign(), 1)) {
                                        //创客修改卡余额
                                        MemberCardSellRelationLevel memberCardSellRelationLevel = memberCardSellRelationLevelService.getOne(
                                                new QueryWrapper<MemberCardSellRelationLevel>()
                                                        .eq(MemberCardSellRelationLevel.ORDER_NO, orderNo)
                                                        .last("limit 1")
                                        );
                                        if (Objects.nonNull(memberCardSellRelationLevel)) {
                                            if (Objects.equals(memberCardSellRelationLevel.getMakerChoose(), 1)) {
                                                Integer amount = 10000;
                                                switch (memberCardSellRelationLevel.getMakerLevel()) {
                                                    case 1:
                                                        break;
                                                    case 2:
                                                        amount = 100000;
                                                        break;
                                                    case 3:
                                                        amount = 200000;
                                                        break;
                                                }
                                                //更新卡余额
                                                boolean b = userMemberCardService.update(
                                                        new UpdateWrapper<UserMemberCard>()
                                                                .set(UserMemberCard.TOTAL_GIVE_AMOUNT, amount)
                                                                .set(UserMemberCard.REMAIN_GIVE_AMOUNT, amount)
                                                                .eq(UserMemberCard.USER_ID, userInfo.getId())
                                                                .eq(UserMemberCard.CARD_TYPE, memberCardEnableOrder.getCardType())
                                                );
                                                if (!b) {
                                                    throw new ServiceException("更新卡余额失败");
                                                }
                                            } else if (Objects.equals(memberCardSellRelationLevel.getMakerChoose(), 2)) {
                                                Integer amount = 0;
                                                //更新卡余额
                                                boolean b = userMemberCardService.update(
                                                        new UpdateWrapper<UserMemberCard>()
                                                                .set(UserMemberCard.TOTAL_GIVE_AMOUNT, amount)
                                                                .set(UserMemberCard.REMAIN_GIVE_AMOUNT, amount)
                                                                .eq(UserMemberCard.USER_ID, userInfo.getId())
                                                                .eq(UserMemberCard.CARD_TYPE, memberCardEnableOrder.getCardType())
                                                );
                                                if (!b) {
                                                    throw new ServiceException("更新卡余额失败");
                                                }
                                            }
                                            //更新有效期
                                            MemberCard memberCard1 = memberCardService.getById(memberCard.getId());
                                            boolean b = memberCardService.update(
                                                    new UpdateWrapper<MemberCard>()
                                                            .set(MemberCard.EXPIRED_DATE, DateUtil.offsetMonth(memberCard1.getExpiredDate(), 3))
                                                            .eq(MemberCard.ID, memberCard.getId())
                                            );
                                            if (!b) {
                                                throw new ServiceException("更新有效期失败");
                                            }
                                        }
                                    }
                                    //抖音活动售卡，修改余额，19.9的价格不送房费
                                    else if(Objects.equals(memberCardEnableOrder.getSign(), 3)){
                                        boolean b = userMemberCardService.update(
                                                new UpdateWrapper<UserMemberCard>()
                                                        .set(UserMemberCard.TOTAL_GIVE_AMOUNT, 0)
                                                        .set(UserMemberCard.REMAIN_GIVE_AMOUNT, 0)
                                                        .eq(UserMemberCard.USER_ID, userInfo.getId())
                                                        .eq(UserMemberCard.CARD_TYPE, memberCardEnableOrder.getCardType())
                                        );
                                        if (!b) {
                                            throw new ServiceException("抖音售卡更新卡余额失败");
                                        }
                                    }
                                } else {
                                    log.error("会员卡enable订单支付回调:jip关系更新失败:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
                                            , orderNo, payTypeEnum.desc(), resultCode, succeeded);
                                }
                                break;
                        }
                    }
                } else {
                    //退款
                    orderHeadService.cancelOrderNoCheck(orderNo, "预订订单失败,订单取消");
                }
                return null;
            });
        } else {
            log.error("会员卡enable订单支付回调:订单不存在或状态不正确:orderNo:{},payTypeEnum:{},resultCode:{},succeeded:{}"
                    , orderNo, payTypeEnum.desc(), resultCode, succeeded);
        }
    }

}
